import { apiConfig } from './apiConfig.js'
import { token } from './token.js'

const send = (url, method, data) => {
	uni.showLoading({
		title: '加载中'
	})
	return new Promise((resolve) => {
		uni.request({
			url: url,
			method: method,
			data: data,
			header: (() => {
				const tokeValue = token.get()
				let config = {
					'Content-Type': 'application/x-www-form-urlencoded'
				}
				if (tokeValue) {
					config[token] = tokeValue
				}
				return config
			})(),
			success: (res) => {
				uni.hideLoading()
				resolve(res)
			}
		})
	})
}

export const request = {
	get: (url, data) => {
		return send(url, 'GET', data)
	},
	post: (url, data, baseUrl) => {
		return send(url, 'POST', data)
	}
}